﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using WinFormsAppTest.Class;
using WinFormsAppTest.pojo;

namespace WinFormsAppTest
{
    internal class TestDao
    {
        String connectionString = @"Data Source=DESKTOP-SQERB5L\MSSQLSERVERR;" +
            "Initial Catalog=hylpersonsystem;" +
            "User ID=hyl;" +
            "Password=123456;";
        //public SqlConnection connect(String aname ,String apassword)
        //{

        //    using (SqlConnection sc = new SqlConnection(connectionString))
        //    {  ///
        //        try
        //        {
        //            sc.Open();

        //            // 检查连接状态
        //            if (sc.State == ConnectionState.Open)
        //            {
        //                Console.WriteLine("数据库连接成功！");
        //                MessageBox.Show("数据库连接成功！");
        //                FindIsUser(aname, apassword);
        //                //sc.Close();
                        
        //                // 连接成功后可以执行其他操作
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            Console.WriteLine("数据库连接失败：" + ex.Message);
        //            MessageBox.Show("数据库连接失败！");
        //            sc.Close();  
        //        }
        //        return sc;
        //    }///
        //}


        //connect 无参
        public SqlConnection connect()
        {

            using (SqlConnection sc = new SqlConnection(connectionString))
            {  ///
                sc.Open();
                try
                {
                    //sc.Open();

                    // 检查连接状态
                    if (sc.State == ConnectionState.Open)
                    {
                        Console.WriteLine("数据库连接成功！");
                        MessageBox.Show("数据库连接成功！");
                        sc.Close();

                        // 连接成功后可以执行其他操作
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("数据库连接失败：" + ex.Message);
                    MessageBox.Show("数据库连接失败！");
                    sc.Close();
                }
                return sc;
            }///
        }
        //



        //判断是否在user表内,登录界面逻辑
        //如果登录成功，则返回用户id
        public int FindIsUser(String uname,String upassword)
        {
            // 在这里执行具体的数据库操作
            // 例如使用 SqlCommand 执行查询语句或执行存储过程
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            using (SqlCommand command = new SqlCommand("SELECT * FROM users", sc))
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int id = reader.GetInt32(0);
                        String username = reader.GetString(1);
                        String password = reader.GetString(2);
                        if(username ==uname && password ==upassword)
                        {
                           // MessageBox.Show("登录成功！");
                            sc.Close();
                            
                            return id;
                        }
                    }
                    //MessageBox.Show("登录失败！");
                    //MessageBox.Show(uname+" "+ upassword);
                    
                    sc.Close();
                    return 0;
                }
            }
        }


       



        //添加用户信息
        public void CreateUser(String uname, String upassword)
        {
            // 在这里执行具体的数据库操作
            // 例如使用 SqlCommand 执行查询语句或执行存储过程
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "insert into users values(@Username,@Password)";
            if(sc.State != ConnectionState.Open)
            {
                MessageBox.Show("注册时，数据库连接失败！");
                return;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Username", uname);
                command.Parameters.AddWithValue("@Password", upassword); 
                command.ExecuteNonQuery();
                //MessageBox.Show("注册成功！");
                sc.Close();
                return;
            }
        }


        //通过用户id获取档案信息
        public List<Files> GetUserFilesById(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from files where userid = @Userid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Userid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<Files> files = new List<Files>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int filesid = reader.GetInt32(0);
                        String msg = reader.GetString(1);
                        String house = reader.GetString(2);
                        String contract = reader.GetString(3);
                        String insure = reader.GetString(4);
                        int userid = id;
                        Files f = new Files(filesid,msg,house,contract,
                                            insure,userid);
                        files.Add(f);
                    }
                    sc.Close();
                    return files;
                }
            }
        }
        //


        //通过用户id获取人事变动信息
        public List<Change> GetUserChangeById(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from change where userid = @Userid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Userid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<Change> change = new List<Change>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int changeid = reader.GetInt32(0);
                        String type = reader.GetString(1);
                        DateTime time = reader.GetDateTime(2);
                        int userid = id;
                        Change f = new Change(changeid, type,time, userid);
                        change.Add(f);
                    }
                    sc.Close();
                    return change;
                }
            }
        }
        //

        //通过用户id获取考核奖惩信息
        public List<Exam> GetUserExamById(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from exam where userid = @Userid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Userid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<Exam> exam = new List<Exam>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int examid = reader.GetInt32(0);
                        String state = reader.GetString(1);
                        String measure = reader.GetString(2);
                        String goal = reader.GetString(3);
                        int userid = id;
                        Exam f = new Exam(examid, state, measure,goal, userid);
                       exam.Add(f);
                    }
                    sc.Close();
                    return exam;
                }
            }
        }
        //


        //通过用户id获取考勤编号信息
        public List<Work> GetUserWorkById(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from work where userid = @Userid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Userid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<Work> work = new List<Work>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int workid = reader.GetInt32(0);
                        int userid = id;
                        Work f = new Work(workid, userid);
                        work.Add(f);
                    }
                    sc.Close();
                    return work;
                }
            }
        }
        //

        //通过用户id获取考勤的签到信息
        public List<Signs> GetUserSignsById(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from signs where userid = @Userid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Userid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<Signs> signs = new List<Signs>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int signid = reader.GetInt32(0);
                        DateTime begintime = reader.GetDateTime(1);
                        DateTime endtime = reader.GetDateTime(2);
                        int workid = reader.GetInt32(3);
                        int userid = id;
                        Signs f = new Signs(signid, begintime, endtime, workid, userid);
                        signs.Add(f);
                    }
                    sc.Close();
                    return signs;
                }
            }
        }
        //

        //通过用户id获取考勤的加班信息
        public List<Overtime> GetUserOvertimeById(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from overtime where userid = @Userid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Userid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<Overtime> overtime = new List<Overtime>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int overid = reader.GetInt32(0);
                        DateTime begintime = reader.GetDateTime(1);
                        DateTime endtime = reader.GetDateTime(2);
                        int workid = reader.GetInt32(3);
                        int userid = id;
                        Overtime f = new Overtime(overid, begintime, endtime, workid, userid);
                        overtime.Add(f);
                    }
                    sc.Close();
                    return overtime;
                }
            }
        }
        //


        //通过用户id获取考勤的出差信息
        public List<Outtime> GetUserOuttimeById(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from outtime where userid = @Userid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Userid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<Outtime> outtime = new List<Outtime>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int outid = reader.GetInt32(0);
                        DateTime begintime = reader.GetDateTime(1);
                        DateTime endtime = reader.GetDateTime(2);
                        String place = reader.GetString(3);
                        int workid = reader.GetInt32(4);
                        int userid = id;
                        Outtime f = new Outtime(outid, begintime, endtime, place,workid, userid);
                        outtime.Add(f);
                    }
                    sc.Close();
                    return outtime;
                }
            }
        }
        //

    }
}
